<template>
  <el-dialog :visible.sync="dialogTableVisible" :append-to-body="true" :lock-scroll="false" :title="isEdit?'Edit':'Create'" @close="handleCloseDetailDialog">
    <el-form ref="postForm" :model="postForm" :rules="rules" label-position="left" label-width="100px" style="margin-left:50px;">

      <el-form-item :label="$t('couponcodefiles.coupon_id')" required prop="coupon_id">
        <el-select v-model="postForm.coupon_id" clearable placeholder="Please Chose" width="600px">
          <el-option v-for="item in couponList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('couponcodefiles.total')" prop="total">
        <el-col :span="20">
          <el-input v-model.number="postForm.total" type="number"/>
        </el-col>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCloseDetailDialog">{{ $t('table.cancel') }}</el-button>
      <el-button type="primary" @click="submitForm('postForm')">{{ $t('table.submit') }}</el-button>
    </div>

  </el-dialog>
</template>
<script>

import couponcodeApi from '@/api/coupon/couponcode'
import ScrollPane from '../../../../../components/ScrollPane/index'

const defaultForm = {
  total: 1,
  coupon_id: '',
  couponList: []
}

export default {
  name: 'Detail',
  components: { ScrollPane },
  data() {
    return {
      postForm: Object.assign({}, defaultForm),
      rules: {
        coupon_id: [{ required: true, message: 'Coupon is required', trigger: 'change' }]
      },
      isEdit: false,
      recordid: 0,
      dialogTableVisible: false,
      menuList: [],
      selectedMenuOptions: [],
      permissionList: [],
      selectedPermissionOptions: [],
      permissionProps: {
        label: 'name',
        value: 'id',
        children: '_child'
      },
      couponList: []
    }
  },
  computed: {
    lang() {
      return this.$store.getters.language
    },
    areaOptions: function() {
      return this.$store.state.locale.areas
    }
  },
  created() {
  },

  mounted() {

  },

  methods: {
    showRecord(id) {
      couponcodeApi.showRecord(id).then(response => {
        const serviceResponse = response.data
        if (serviceResponse.code === 0) {
          this.couponList = serviceResponse.data.couponList
        }
      }).catch(err => {
        console.log(err)
      })
    },
    handleCreateRecord() {
      couponcodeApi.createRecord().then(response => {
        const serviceResponse = response.data
        if (serviceResponse.code === 0) {
          this.postForm = Object.assign({}, this.postForm, serviceResponse.data.record)
          this.couponList = serviceResponse.data.couponList
        }
      }).catch(err => {
        console.log(err)
      })
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.editId > 0) {
            couponcodeApi.updateRecord(this.editId, this.postForm).then(response => {
              const serviceResponse = response.data
              if (serviceResponse.code === 0) {
                this.$message({
                  type: 'success',
                  message: serviceResponse.msg
                })
                this.handleCloseDetailDialog()
              } else {
                this.$message({
                  type: 'error',
                  message: serviceResponse.msg
                })
              }
            })
          } else {
            couponcodeApi.storeRecord(this.postForm).then(response => {
              const serviceResponse = response.data
              if (serviceResponse.code === 0) {
                this.$message({
                  type: 'success',
                  message: serviceResponse.msg
                })
                this.handleCloseDetailDialog()
              } else {
                this.$message({
                  type: 'error',
                  message: serviceResponse.msg
                })
              }
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(postForm) {
      this.$refs[postForm].resetFields()
    },

    handShowDetailDialog(id) {
      this.selectedMenuOptions = []
      this.menuList = []
      this.selectedPermissionOptions = []
      this.permissionList = []
      if (id > 0) {
        this.isEdit = true
        this.editId = id
        this.showRecord(id)
      } else {
        this.isEdit = false
        this.handleCreateRecord()
        this.postForm = Object.assign({}, defaultForm)
      }
      this.dialogTableVisible = true
    },
    handleCloseDetailDialog() {
      this.resetForm('postForm')
      this.editId = 0
      this.$emit('indexRecord')
      this.dialogTableVisible = false
    }

  }

}
</script>
